<template>
  <div>
    <h4 class="lighter">
      <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
      <router-link to="/business/course" class="pink"> {{course.name}} </router-link>
    </h4>
    <hr>
    <p>
      <router-link to="/business/course" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-arrow-left"></i>
        返回课程
      </router-link>
      &nbsp;
      <button v-on:click="add()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;
      <button @click="list(1)" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh red2"></i>
      刷新
    </button>
    </p>

    <pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
        <thead>
        <tr>
          <th>章节ID</th>
          <th>章节名称</th>
<!--          <th>所属课程</th>-->
          <th>操作</th>
        </tr>
        </thead>

        <tbody>
        <tr v-for="chapter in chapters">
          <td>{{ chapter.id }}</td>
          <td>{{ chapter.name }}</td>
<!--          <td>{{ chapter.courseId}}</td>-->
          <td>
            <div class="hidden-sm hidden-xs btn-group">
              <button @click="toSection(chapter)" class="btn btn-white btn-xs btn-info btn-round">
               小节
              </button>&nbsp;
              <button @click="edit(chapter)" class="btn btn-white btn-xs btn-info btn-round">
               编辑
              </button>&nbsp;
              <button @click="del(chapter.id)" class="btn btn-white btn-xs btn-warning btn-round">
                删除
              </button>
            </div>

            <div class="hidden-md hidden-lg">
              <div class="inline pos-rel">
                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                  <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                </button>

                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                  <li>
                    <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                  <input v-model="chapter.name" class="form-control" placeholder="名称">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">课程</label>
                <div class="col-sm-10">
                  <p class="form-control-static">{{course.name}}</p>
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button @click="save" type="button" class="btn btn-primary">保存</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
  </div>
</template>

<script>
import Pagination from "../../components/pagination"
export default {
  components: {Pagination},
  name: 'chapter',
  data: function (){
    return {
      chapter:{},
      chapters: [],
      course:{}
    }
  },
  mounted() {
    //页面激活  方法一
    // this.$parent.activeSidebar("business-chapter-sidebar")
    let _this = this;
    _this.$refs.pagination.size = 5;
    let course = SessionStorage.get(SESSION_KEY_COURSE)||{};
    if (Tool.isEmpty(course)){
      _this.$router.push("/welcome")
    }
    _this.course = course;
    console.log("course",course)
    _this.list(1);//直接页面加载中就渲染大章列表
  },
  methods:{
    /**
     * 点击【新增】弹框
     */
    add() {
      let _this = this;
      _this.chapter = {};//解决点击编辑后再点击新增时候会带出来的数据参数
      $("#form-modal").modal("show");
      // $(".modal").modal({backdrop:'static'});
    },
    /**
     *列表展示
     */
    list(page){
      let _this = this;
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/getChapterList',{
        page:page,
        size:_this.$refs.pagination.size,
        courseId:_this.course.id
      }).then((response)=>{
           Loading.hide();
        console.log("查询大章节列表：", response)
        let resp = response.data
        _this.chapters = resp.data.list
        _this.$refs.pagination.render(page, resp.data.total);
      })
    },
    /**
     * 点击保存/修改大章信息
     */
    save(){
      let _this = this;
      //保存校验
      if (!Validator.require(_this.chapter.name,"名称")||
          !Validator.length(_this.chapter.courseId,"课程Id",1,8)){
        return;
      }
      _this.chapter.courseId = _this.course.id;
      Loading.show();
      console.log("有什么东西id："+ _this.chapter.id)
      console.log("有什么东西name："+ _this.chapter.name)
      console.log("有什么东西courseId："+ _this.chapter.courseId)
      _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/save',
        _this.chapter
      ).then((response)=>{
        console.log("保存大章节列表：", response)
        let resp = response.data
        if (resp.code===0){
          Loading.hide()
          $("#form-modal").modal("hide");
          Toast.success("保存成功")
          _this.list(1);
        }else {
          Loading.hide()
          Toast.warning(resp.msg)
        }
      })
    },
    //点击编辑按钮，弹框
    edit(chapter){
      let _this = this;
      _this.chapter = Object.assign({}, chapter)//tags是所编辑本行内数据对象 ,解决对象引用同地址问题
      $("#form-modal").modal("show");
    },
    //点击删除按钮
    del(chapterId){
      let _this = this;
      Confirm.show("删除dazhang之后不可恢复，确认删除？",function () {
        Loading.show();
        _this.$ajax.delete(process.env.VUE_APP_SERVER+'/business/admin/delete/'+chapterId).then((response)=>{
          console.log("删除某一大章节信息：", response)
          let resp = response.data
          if (resp.code===0){
            Loading.hide();
            _this.list(1);
            Toast.success("删除成功");
          }else {
            Toast.error("删除失败");
          }
        })
      })
    },
    /**
     * 点击【小节】
     */
    toSection(chapter) {
      let _this = this;
      SessionStorage.set(SESSION_KEY_CHAPTER, chapter);
      _this.$router.push("/business/section");
    }
  }
}
</script>